package com.hotchpotch.services;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.hotchpotch.bean.KillServiceLogBean;
import com.hotchpotch.util.Logg;
import com.raizlabs.android.dbflow.config.FlowManager;
import com.raizlabs.android.dbflow.structure.ModelAdapter;

public class NoKilledService extends Service {
    private static final String TAG = "NoKilledService";
    private ModelAdapter<KillServiceLogBean> adapter;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Logg.e(TAG, "service is running!" + "  时间戳:" + System.currentTimeMillis());
        adapter = FlowManager.getModelAdapter(KillServiceLogBean.class);
        KillServiceLogBean bean = new KillServiceLogBean();
        bean.setTime(System.currentTimeMillis() + "");
        bean.setContent("服务运行");
        adapter.insert(bean);
        return Service.START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Logg.e(TAG, "service is killed!");
        KillServiceLogBean bean = new KillServiceLogBean();
        bean.setTime(System.currentTimeMillis() + "");
        bean.setContent("服务被杀死");
        adapter.insert(bean);
        Intent intent = new Intent(this, NoKilledService.class);
        startService(intent);
    }
}
